﻿#include "SpeedManageWidget.h"
#include "ui_SpeedManageWidget.h"
#include "CommonFunc.h"
#include "DataInfo.h"
#include <GroupSpeedWidget.h>
#include <ScrollSpeedWidget.h>

SpeedManageWidget::SpeedManageWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SpeedManageWidget)
{
    ui->setupUi(this);

    appendTypeToFunc(XShowSpeed, std::bind(&SpeedManageWidget::showSpeedInfo, this));

    _verticalLayout = new QVBoxLayout();
    _verticalLayout->setSpacing(20);
    _verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    _verticalLayout->setContentsMargins(40, 40, 40, 40);
    _verticalSpacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Preferred);

    _verticalLayout->addItem(_verticalSpacer);
    ui->speedWidget->setLayout(_verticalLayout);
}

SpeedManageWidget::~SpeedManageWidget()
{
    delete ui;
}

void SpeedManageWidget::showSpeedInfo()
{
    if (g_iProductLineID == -1 || g_iGateWayID == -1)
    {
        clearSpeedData();
        return;
    }
    clearSpeedData();

    if (!g_strGateStateInfo.isEmpty())
    {
        ui->toolButton_title->setText(g_strGateStateInfo);
    }

    _verticalLayout->removeItem(_verticalSpacer);

    int maxValue = -1;
    QMapIterator<int, QList<Sensor> > iter(g_mapGroupIdToListSensor);
    while (iter.hasNext()) {
        iter.next();
        int gID = iter.key();

        GroupSpeedWidget *pGroupWidget = new GroupSpeedWidget(this);
        pGroupWidget->setMaximumHeight(330);
        pGroupWidget->getScrollSpeedWidget()->setGroupIDAndSensor(gID, iter.value(), maxValue);
        _verticalLayout->addWidget(pGroupWidget);
        _vecGroupWidget.push_back(pGroupWidget);

    }

    _verticalLayout->addItem(_verticalSpacer);
    for (int i = 0; i < _vecGroupWidget.size(); ++i)
    {
        _vecGroupWidget[i]->getScrollSpeedWidget()->setMaxValue(maxValue);
    }
}

void SpeedManageWidget::clearSpeedData()
{
    ui->toolButton_title->setText(QStringLiteral("产线-网关"));
    for (int i = 0; i < _vecGroupWidget.size(); ++i)
    {
        _verticalLayout->removeWidget(_vecGroupWidget[i]);
        _vecGroupWidget[i]->hide();
        _vecGroupWidget[i]->deleteLater();
    }
    _vecGroupWidget.clear();
}

void SpeedManageWidget::on_pushButton_returnUp_clicked()
{
    executeFuncFromID(XUI_DeviceManage);
}

void SpeedManageWidget::on_pushButton_returnTop_clicked()
{
//    for (int i = 0; i < _vecGroupWidget.size(); ++i)
//    {
//        ui->pushButton_returnTop->setText(QString::number(_vecGroupWidget[i]->height()));
//        break;
//    }
//    return;
    executeFuncFromID(XUI_Login);
}
